package com.wxp.alice.lanfilemaster;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.alice.lanfilemaster.Server.HttpServer;
import com.wxp.alice.lanfilemaster.Utils.NetworkUtils;
import com.wxp.alice.lanfilemaster.Utils.PermissionUtils;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private final String[] REQUEST_PERMISSIONS = {
            android.Manifest.permission.ACCESS_COARSE_LOCATION,
            android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.INTERNET,
            android.Manifest.permission.ACCESS_NETWORK_STATE,
            android.Manifest.permission.ACCESS_WIFI_STATE
    };

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // TODO: Implement your logic here
        TextView textView = findViewById(R.id.textHello);
        switch (NetworkUtils.getNetworkType(this)) {
            case NONE:
                textView.setText("No network connection");
                break;
            case WIFI:
                textView.setText("Connected to Wifi: " + NetworkUtils.getCurrentWifiSSID(this));
                break;
            case MOBILE:
                textView.setText("Connected to Mobile Data");
                break;
            default:
                textView.setText("Unknown network type");
                break;
        }

        HttpServer httpServer = new HttpServer("127.0.0.1", 12345);
        try {
            httpServer.start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        requestPermissions(REQUEST_PERMISSIONS ,1);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            if (!PermissionUtils.hasPermissionsGranted(this, REQUEST_PERMISSIONS)) {//返回false代表申请失败
                Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
                this.finish();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}